(原創) 如何對程式片段進行效能測試(benchmark) (C/C++) (C)
1
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : Benchmark.cpp
5
Compiler : Visual C++ 8.0
6
Description : Demo how to caculate processing time of specified function.
7
*/
8
#include <iostream>
9
#include <ctime>
10
11
void sub();
12
13
int main() {
14
// clock_t is typedef of (long)
15
clock_t t = clock();
16
17
sub();
18
19
t = clock() -t;
20
21
// CLOCKS_PER_SEC is macro
22
std::cout << "Process time:" << (double)t/CLOCKS_PER_SEC << " sec (" << t << " clocks)" << std::endl;
23
24
return 0;
25
}
26
27
void sub() {
28
for(int i=0; i != 10000; ++i) {
29
for(int j=0; j != 100000; ++j) {
30
;
31
}
32
}
33
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

See Also
(原創) 如何對程式片段進行效能測試(benchmark) (.NET) (C#)
Reference
C程式設計 500個應用範例技巧大全集,平田 豐,P.468